package com.gdk.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gdk.utils.CommonUtils;
import com.gdk.utils.QRCodeUtil;
import com.gdk.utils.QRTest;
import com.gdk.utils.XVDateUtils;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;


/**
 * controller
 * */

@Controller
public class QRController {
	
	/**
	 * 生成二维码
	 * @author xuwei
	 * @return String 
	 * @throws Exception 
	 * */
	@RequestMapping("/getQRCode.do")
	public void getQRCode(@RequestParam Map<String,Object> map, HttpServletRequest request,HttpServletResponse response) throws Exception {
		
		// 存放在二维码中的内容
			String text = map.get("url_str").toString();
			int millis = XVDateUtils.getCurrenTimeMillis();
			
			/*String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";   
	        char[] c = s.toCharArray();   
	        Random random = new Random();   
	        for( int i = 0; i < 8; i ++) {   
	            System.out.println(c[random.nextInt(c.length)]);   
	        }  */
			String chars = "abcdefghijklmnopqrstuvwxyz";
			System.out.println(chars.charAt((int)(Math.random() * 26)));
			char ra = chars.charAt((int)(Math.random() * 26));
			char rb = chars.charAt((int)(Math.random() * 26));
			char rc = chars.charAt((int)(Math.random() * 26));
			char rd = chars.charAt((int)(Math.random() * 26));
			// 嵌入二维码的图片路径
			//String imgPath = "G:/qrCode/"+millis+".jpg";
			
			//得到保存的目录
			String classpath = QRTest.class.getResource("/").getPath().replaceFirst("/", "");
			System.out.println("classpath:"+classpath);
			String savePath = classpath.replaceAll("wlAdmin/WEB-INF/classes/", "temp_img/");
			//防止传项目文件夹丢失无法上传图片先创建临时图片文件夹
			File file = new File(savePath);
			//判断文件夹是否存在   不存在才创建
			if (!file.exists()) {
				System.out.println("目标文件夹已创建");
	            file.mkdirs();  
	        }  	
			System.out.println("保存路径 :"+savePath);
			String aa = file+"/"+millis+"_"+ra+rb+rc+rd+"_"+CommonUtils.getUUID()+".jpg";
			
	
			System.out.println("aaa : "+aa);
			// 生成的二维码的路径及名称
			String destPath = aa;
			//生成二维码
			//QRCodeUtil.encode(text, imgPath, destPath, true);
			QRCodeUtil.encode(text,"",destPath, true);
			// 解析二维码
			//String str = QRCodeUtil.decode(destPath);
			// 打印出解析出的内容
			//System.out.println(str);
	 

		//return aa;
			
			//return "redirect:"+aa;

			//response.sendRedirect(aa);
			//直接返回图片到首页
			 FileInputStream fis = null;
		        response.setContentType("image/jpg");
		        try {
		            OutputStream out = response.getOutputStream();
		            File ffile = new File(aa);
		            fis = new FileInputStream(ffile);
		            byte[] b = new byte[fis.available()];
		            fis.read(b);
		            out.write(b);
		            //返回后删除图片
		            ffile.delete();
		            out.flush();
		        } catch (Exception e) {
		             e.printStackTrace();
		        } finally {
		            if (fis != null) {
		                try {
		                   fis.close();
		                } catch (IOException e) {
		                e.printStackTrace();
		            }   
		              }
		        }
	}
	
}